package com.sskj.givecoin.p.activity;

import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.AppVersionBean;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.bean.PublicData;
import com.sskj.givecoin.m.bean.UserInfo;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.APKVersionCodeUtils;
import com.sskj.givecoin.utils.SPUtils;
import com.sskj.givecoin.v.activity.MainActivity;


/**
 * Created by Lee on 2018/1/25 0025.
 */

public class MainPresenter extends BasePresenter<MainActivity> {

    public void upDateUserInfo(String mobile) {
        OkGo.<HttpData<UserInfo>>post(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .tag(mView)
                .params("mobile", mobile)
                .execute(new JsonCallBack<HttpData<UserInfo>>(this, false) {
                    @Override
                    public void _onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_IS_SHOP, httpData.getData().is_shop);
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, httpData.getData().getStatus());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS_HIGH, httpData.getData().getAuth_status());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_PAY_SIZE, httpData.getData().getPay_list());

                            if (httpData.getData().getRealname() != null) {
                                SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME, httpData.getData().getRealname());
                            }
                            if (httpData.getData().getIdcard() != null) {
                                SPUtils.put(MyAppLication.getApplication(), Constants.SP_IDCORD, httpData.getData().getIdcard());
                            }
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_JGCODE, httpData.getData().getJigou_code());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_USDT, httpData.getData().getUsdt());

                            mView.updateUserInfo(httpData.getData());
                        }
                    }
                });
    }


    public void  getNotice() {
        OkGo.<HttpData<PublicData>>post(HttpConfig.BASE_URL + HttpConfig.PUBLIC_DATA)
                .tag(mView)
                .execute(new JsonCallBack<HttpData<PublicData>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<PublicData>> response) {
                        HttpData<PublicData> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            if (!TextUtils.isEmpty(httpData.getData().getTpgg())) {
                                mView.showNotice(httpData.getData().getTpgg());
                            }
                        }
                    }
                });

    }
    public void getVersion() {
        OkGo.<HttpData<AppVersionBean>>post(HttpConfig.BASE_URL + HttpConfig.VERSION)
                .tag(mView)
                .params("version", APKVersionCodeUtils.getVerName(MyAppLication.getApplication()))
                .params("type", "1")
                .tag(mView)
                .execute(new JsonCallBack<HttpData<AppVersionBean>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<AppVersionBean>> response) {
                        HttpData<AppVersionBean> httpData = response.body();
                        mView.onVersionDataSuccess(httpData.getData());
                    }
                });

    }

}
